package com.milo;


import com.service.ServiceFunction;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

//OPC UA协议对象实体类
@Service("opcUAClientService")
public class OpcUAClientServiceImpl implements OpcUAClientService {

    @Autowired
    private  ServiceFunction serviceFunction;


    /**
     * 覆盖接口的方法，建立和OPC UA的服务
     */
    @Override
    public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
        // 同步建立连接
        client.connect().get();

        //执行具体的业务代码
        serviceFunction.doSomething(client);
        serviceFunction.doSomething(client, future);

        // 异步读取数据
//        readTagData(client).thenAccept(values -> {
//            DataValue nodeId_Tag1 = values.get(0);
//            DataValue nodeId_Tag2 = values.get(1);
//            System.out.println("#########x1=" + nodeId_Tag1.getValue().getValue());
//            System.out.println("#########x2=" + nodeId_Tag2.getValue().getValue());
//            future.complete(client);
//        });


        //不使用异步处理问题
//        NodeId nodeId_Tag1 = new NodeId(2, "my.device.x1");
//        NodeId nodeId_Tag2 = new NodeId(2, "my.device.x2");
//        CompletableFuture<DataValue> dataValueCompletableFuture0 = client.readValue(0.0, TimestampsToReturn.Both, nodeId_Tag1);
//        CompletableFuture<DataValue> dataValueCompletableFuture1 = client.readValue(0.0, TimestampsToReturn.Both, nodeId_Tag2);
//        DataValue tag0 = dataValueCompletableFuture0.get();
//        DataValue tag1 = dataValueCompletableFuture1.get();
//        System.out.println("#########Tag1=" + tag0.getValue().getValue());
//        System.out.println("#########Tag2=" + tag1.getValue().getValue());


    }

    /**
     * 读取标签点的数据
     */
//    private CompletableFuture<List<DataValue>> readTagData(OpcUaClient client) {
//        NodeId nodeId_Tag1 = new NodeId(2,"my.device.x1");
//        NodeId nodeId_Tag2 = new NodeId(2, "my.device.x2");
//
//        List<NodeId> nodeIds = ImmutableList.of(nodeId_Tag1, nodeId_Tag2);
//        return client.readValues(0.0, TimestampsToReturn.Both, nodeIds);
//    }
}



